package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shwread.android.qysw.activity.AllHealthSourchActivity;
import com.shwread.android.qysw.activity.CommonWebActivity;
import com.shwread.android.qysw.activity.HealthSourchActivity;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000243.R;
import com.shwread.android.ui.widget.ChartView;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.geometerplus.android.util.QyreaderPerferencesUtil;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import base.tina.core.task.infc.ITaskResult;
import logic.constants.DefaultConsts;

/**
 * Created by Administrator on 2016/3/2.
 */
public class HealthPage extends
        AbstractUIPage<MainActivity>
        implements
        View.OnClickListener {
    private static HealthPage instance;

    private TextView tvDay, tvWeek, tvMonth, tvYear;
    private RelativeLayout allhealth;
    private RelativeLayout healthsourch;
    private RelativeLayout healthsay;
    private Button healthback;
    private ChartView chartView, chartView2, chartView3, chartView4;
    private TextView tvWalkNum, tvAvgWalkNum;
    private int width,height,XPoint,YPoint,YScale,YLength;
    private double x,y;
    private int data1, data2, data3, data4, data5, daydata;
    private int sumData = 0;
    private Set<String> datas = new HashSet<String>();
    private Set<String> data = new HashSet<String>();
    private Set<String> currentDatas, currentData;
    private String[] s1 = new String[10];
    private String[] s2 = new String[10];
    private String[] weekTime = new String[7];
    private String[] monthTime = new String[30];
    private Calendar c;


    public static HealthPage getInstance(MainActivity t) {
        if (instance == null) {
            instance = new HealthPage(t);
        } else {
            instance.setActivity(t);
        }
        return instance;
    }

    private HealthPage(MainActivity t) {
        super(t);
    }

    @Override
    public <E extends Activity> View createView(E arg0, int arg1) {
        if (null != curMyView) return curMyView;
        curMyView = bActivity.getLayoutInflater().inflate(R.layout.activity_health_dataall, null);
        WindowManager wm = bActivity.getWindowManager();
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();
        y = height/1920.0;
        x = width/1080.0;
        XPoint = (int)(x*40);
        YPoint = (int)(y*220);
        YScale = (int)(y*45);
        YLength = (int)(y*240);
        c = Calendar.getInstance();
        getTime();
        findViews();
        initListener();
        initData();
        return curMyView;
    }

    private void getTime() {
        java.util.Date date = new java.util.Date();
        long times = date.getTime();
        for (int i = 6; i >= 0; i--) {
            java.util.Date date1 = new java.util.Date(times - 86400000 * i);
            c.setTime(date1);
            if (i == 6 || i == 0) {
                weekTime[i] = c.get(c.MONTH) + "月" + c.get(c.DAY_OF_MONTH) + "日";
            } else {
                weekTime[i] = c.get(c.DAY_OF_MONTH) + "";
            }
        }
        for (int i = 29; i >= 0; i--) {
            java.util.Date date2 = new java.util.Date(times - 86400000 * i);
            c.setTime(date2);
            if (i == 0 || i == 29) {
                monthTime[i] = c.get(c.MONTH) + "月" + c.get(c.DAY_OF_MONTH) + "日";
            } else {
                monthTime[i] = c.get(c.DAY_OF_MONTH) + "";
            }
        }
    }

    private void initData() {
        for (int i = 0; i < 5; i++) {
            Random random = new Random();
            data1 = random.nextInt(1000) % 501 + 500;
            data2 = random.nextInt(3000) % 2001 + 1000;
            data3 = random.nextInt(5000) % 2001 + 3000;
            data4 = random.nextInt(9000) % 4001 + 5000;
            data5 = random.nextInt(10000) % 1001 + 9000;
            daydata = data1 + data2 + data3 + data4 + data5;
            sumData = sumData + daydata;
            datas.add(daydata + "");
        }
        tvWalkNum.setText(daydata + "步");
        tvAvgWalkNum.setText("日平均值:" + sumData / 5 + "");
        currentDatas = QyreaderPerferencesUtil.getInstance(context, "0").getHealthDayTotalData();
        if (currentDatas == null) {
            QyreaderPerferencesUtil.getInstance(context, "0").setHealthDayTotalData(datas);
            currentDatas = datas;
        }
        currentData = QyreaderPerferencesUtil.getInstance(context, "0").getHealthDayData();
        if (currentData == null) {
            data.add(data1 + "");
            data.add(data2 + "");
            data.add(data3 + "");
            data.add(data4 + "");
            data.add(data5 + "");
            QyreaderPerferencesUtil.getInstance(context, "0").setHealthDayData(data);
            currentData = data;
        }
        int i = 0;
        for (String s : currentData) {
            s1[i++] = s;
        }
        int j = 0;
        for (String s : currentDatas) {
            s2[j++] = s;
        }


        chartView.SetInfo(1,XPoint,YPoint,YScale,YLength, (width - (int)(210*x)) / 4, width - (int)(200*x), new String[]{"0时", "6时", "12时", "18时", "0时"}, //X刻度
                new String[]{"", "2000", "4000", "6000", "8000", "10000"},//Y刻度
                new String[]{s1[0] + "", s1[1] + "", s1[2] + "", s1[3] + "", s1[4] + ""},//数据
                ""
        );
    }

    private void initListener() {
        healthback.setOnClickListener(this);
        allhealth.setOnClickListener(this);
        healthsourch.setOnClickListener(this);
        healthsay.setOnClickListener(this);
        tvDay.setOnClickListener(this);
        tvWeek.setOnClickListener(this);
        tvMonth.setOnClickListener(this);
        tvYear.setOnClickListener(this);
    }

    private void findViews() {
        allhealth = (RelativeLayout) curMyView.findViewById(R.id.all_health_sourch);
        healthsourch = (RelativeLayout) curMyView.findViewById(R.id.health_sourch);
        healthback = (Button) curMyView.findViewById(R.id.common_header_back);
        chartView = (ChartView) curMyView.findViewById(R.id.health_chart1);
        chartView2 = (ChartView) curMyView.findViewById(R.id.health_chart2);
        chartView3 = (ChartView) curMyView.findViewById(R.id.health_chart3);
        chartView4 = (ChartView) curMyView.findViewById(R.id.health_chart4);
        healthsay = (RelativeLayout) curMyView.findViewById(R.id.health_say_you);
        tvDay = (TextView) curMyView.findViewById(R.id.tv_day);
        tvWeek = (TextView) curMyView.findViewById(R.id.tv_week);
        tvMonth = (TextView) curMyView.findViewById(R.id.tv_month);
        tvYear = (TextView) curMyView.findViewById(R.id.tv_year);
        tvWalkNum = (TextView) curMyView.findViewById(R.id.health_walk_num);
        tvAvgWalkNum = (TextView) curMyView.findViewById(R.id.health_day_avg);

    }

    @Override
    public View updateView(int i, ITaskResult iTaskResult) {
        return null;
    }

    @Override
    public void notifyView(int i, Bundle bundle) {

    }

    @Override
    public int enter(IUIPage<?> iuiPage) {
        return 0;
    }

    @Override
    public int leave(IUIPage<?> iuiPage) {
        return 0;
    }

    @Override
    public void setStatus(int i) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_header_back:
                bActivity.finish();
                break;
            case R.id.tv_day:
                clickDay();
                break;
            case R.id.tv_week:
                clickWeek();
                break;
            case R.id.tv_month:
                clickMonth();
                break;
            case R.id.tv_year:
                clickYear();
                break;
            case R.id.all_health_sourch:
                Intent intent = new Intent(bActivity, AllHealthSourchActivity.class);
                bActivity.startActivity(intent);
                break;
            case R.id.health_sourch:
                Intent intent1 = new Intent(bActivity, HealthSourchActivity.class);
                bActivity.startActivity(intent1);
                break;
            case R.id.health_say_you:
                Intent intent2 = new Intent(bActivity, CommonWebActivity.class);
                intent2.putExtra(DefaultConsts.title_s, "产品描述说明");
                intent2.putExtra(DefaultConsts.req_url_s, "http://42.121.255.86/qyyd/file/10000152/index.html");
                bActivity.startActivity(intent2);
                break;
        }
    }

    private void clickDay() {
        chartView.SetInfo(1,XPoint,YPoint,YScale,YLength, (width - (int)(210*x)) / 4, width - (int)(200*x), new String[]{"0时", "6时", "12时", "18时", "0时"}, //X刻度
                new String[]{"", "2000", "4000", "6000", "8000", "10000"},//Y刻度
                new String[]{s1[0] + "", s1[1] + "", s1[2] + "", s1[3] + "", s1[4] + ""},//数据
                ""
        );
        chartView.setVisibility(View.VISIBLE);
        chartView2.setVisibility(View.GONE);
        chartView3.setVisibility(View.GONE);
        chartView4.setVisibility(View.GONE);
        tvDay.setTextColor(bActivity.getResources().getColor(R.color.common_white));
        tvDay.setBackgroundResource(R.drawable.bg_tab_null);
        tvWeek.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvWeek.setBackgroundResource(R.drawable.bg_tab);
        tvMonth.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvMonth.setBackgroundResource(R.drawable.bg_tab);
        tvYear.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvYear.setBackgroundResource(R.drawable.bg_tab);
    }

    private void clickWeek() {
        chartView2.SetInfo(2,XPoint,YPoint,YScale,YLength, (width - (int)(210*x)) / 6, width - (int)(200*x), new String[]{weekTime[6], weekTime[5], weekTime[4], weekTime[3], weekTime[2], weekTime[1], weekTime[0]},
                new String[]{"", "10000", "20000", "30000", "40000", "50000"},
                new String[]{"0", "0", s2[0] + "", s2[1] + "", s2[2] + "", s2[3] + "", s2[4] + ""},
                ""
        );
        chartView.setVisibility(View.GONE);
        chartView2.setVisibility(View.VISIBLE);
        chartView3.setVisibility(View.GONE);
        chartView4.setVisibility(View.GONE);
        tvDay.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvDay.setBackgroundResource(R.drawable.bg_tab);
        tvWeek.setTextColor(bActivity.getResources().getColor(R.color.common_white));
        tvWeek.setBackgroundResource(R.drawable.bg_tab_null);
        tvMonth.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvMonth.setBackgroundResource(R.drawable.bg_tab);
        tvYear.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvYear.setBackgroundResource(R.drawable.bg_tab);
    }

    private void clickMonth() {
        chartView3.SetInfo(3,XPoint,YPoint,YScale,YLength,(width - (int)(210*x)) / 29, width - (int)(200*x), new String[]{monthTime[29], monthTime[28], monthTime[27], monthTime[26], monthTime[25], monthTime[24], monthTime[23], monthTime[22], monthTime[21], monthTime[20], monthTime[19], monthTime[18], monthTime[17], monthTime[16], monthTime[15], monthTime[14], monthTime[13], monthTime[12], monthTime[11], monthTime[10], monthTime[9], monthTime[8], monthTime[7], monthTime[6], monthTime[5], monthTime[4], monthTime[3], monthTime[2], monthTime[1], monthTime[0]},
                new String[]{"", "10000", "20000", "30000", "40000", "50000"},
                new String[]{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", s2[0] + "", s2[1] + "", s2[2] + "", s2[3] + "", s2[4] + ""},
                ""
        );
        chartView.setVisibility(View.GONE);
        chartView2.setVisibility(View.GONE);
        chartView3.setVisibility(View.VISIBLE);
        chartView4.setVisibility(View.GONE);
        tvDay.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvDay.setBackgroundResource(R.drawable.bg_tab);
        tvWeek.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvWeek.setBackgroundResource(R.drawable.bg_tab);
        tvMonth.setTextColor(bActivity.getResources().getColor(R.color.common_white));
        tvMonth.setBackgroundResource(R.drawable.bg_tab_null);
        tvYear.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvYear.setBackgroundResource(R.drawable.bg_tab);
    }

    private void clickYear() {
        chartView.setVisibility(View.GONE);
        chartView2.setVisibility(View.GONE);
        chartView3.setVisibility(View.GONE);
        chartView4.setVisibility(View.VISIBLE);
        tvDay.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvDay.setBackgroundResource(R.drawable.bg_tab);
        tvWeek.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvWeek.setBackgroundResource(R.drawable.bg_tab);
        tvMonth.setTextColor(bActivity.getResources().getColor(R.color.common_topheader_bg));
        tvMonth.setBackgroundResource(R.drawable.bg_tab);
        tvYear.setTextColor(bActivity.getResources().getColor(R.color.common_white));
        tvYear.setBackgroundResource(R.drawable.bg_tab_null);
    }

}
